<%@page import="com.sun.org.apache.xml.internal.utils.SuballocatedByteVector"%>
<%@page import="com.sun.xml.internal.txw2.Document"%>
<%@page import="java.io.Console"%>
<%@page import="db.GoodDao"%>
<%@page import="model.Goods"%>
<%@page import="java.util.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
	<link href="css/main.css" rel="stylesheet" type="text/css">
	<script type="text/javascript" src="js/lhgcore.js"></script>
   <script type="text/javascript" src="js/lhgdialog.js"></script>
   
    <script language="javascript" type="text/javascript">
        function selflog_show(id)
      { 
         var num =  document.getElementById("number").value; 
         J.dialog.get({id: 'haoyue_creat',title: '购物成功',width: 600,height:400, link: '<%=path%>/CartServlet?id='+id+'&num='+num+'&action=add', cover:true});

      }
      function add()//增加数量+
      {
         var num = parseInt(document.getElementById("number").value);
         if(num<100)
         {
            document.getElementById("number").value = ++num;
         }
      }
      function sub()//减少数量图标-
      {
         var num = parseInt(document.getElementById("number").value);
         if(num>1)
         {
            document.getElementById("number").value = --num;
         }
      }
     
    </script>
	
    <style type="text/css">
	   hr{
	     
	     border-color:FF7F00; 
	   }
	   
	   div{
	      float:left;
	      margin-left: 30px;
	      margin-right:30px;
	      margin-top: 5px;
	      margin-bottom: 5px;
	     
	   }
	   div dd{
	      margin:0px;
	      font-size:10pt;
	   }
	   div dd.dd_name
	   {
	      color:blue;
	   }
	   div dd.dd_city
	   {
	      color:#000;
	   }
	   div #cart
	   {
	     margin:0px auto;
	     text-align:right; 
	   }
	   span{
	     padding:0 2px;border:1px #c0c0c0 solid;cursor:pointer;
	   }
	   a{
	      text-decoration: none; 
	   }
	</style>
  </head>

<body>
<h1 align="center" style="color: pink">商品详情</h1>
 <a href="index.jsp">首页</a> >>  <a href="index.jsp">商品列表</a>
    <hr>
    <center>
    <table width="750" height="60" cellpadding="0" cellspacing="0" border="0">
      <tr>
        <td>
          
          <!-- 商品详情 -->
           <% 
               GoodDao goodDao=new GoodDao();
               Goods goods=goodDao.findById(Integer.parseInt(request.getParameter("id")));
               
               if(goods!=null){
           %> 
           <td width="70%" valign="top"> 
           <table> 
           <tr>
               <td rowspan="4">

               <img src="images/<%=goods.getPicture() %>"width="200" height="160"/>
               </td>
           </tr>
                <tr>
                   <td><B><%=goods.getName() %></B></td>
                 </tr>
                 <tr>
                   <td>产地：<%=goods.getCity() %></td>  
                 </tr>
                 <tr>
                   <td>价格：<%=goods.getPrice() %></td> 
                 </tr>
                 <tr>
                    <td>购买数量：<span id="sub" onclick="sub();">-</span><input type="text" id="number" name="number" value="1" size="2"/><span id="add" onclick="add();">+</span></td>
                    </tr>
             </table>
             <div id="cart">
                 <img src="images/buy_now.png">
                <a href="javascript:selflog_show(<%=goods.getId()%>)"><img src="images/in_cart.png"></a>
                <a href="CartServlet?action=show"><img src="images/view_cart.jpg"/></a>
                 
             </div>
          <%
            	   }
          %>
          <%
          String list ="";
          //从客户端获得Cookies集合
          Cookie[] cookies = request.getCookies();
          //遍历这个Cookies集合
          if(cookies!=null&&cookies.length>0)
          {
              for(Cookie c:cookies)
              {
                  if(c.getName().equals("ListViewCookie"))
                  {
                     list = c.getValue();
                  }
              }
          }
          
          list+=request.getParameter("id")+"-";
          //如果浏览记录超过1000条，清零.
          String[] arr = list.split("-");
          if(arr!=null&&arr.length>0)
          {
              if(arr.length>=1000)
              {
                  list="";
              }
          }
         Cookie cookie = new Cookie("ListViewCookie",list);
         response.addCookie(cookie);
             %>
          <!-- 浏览过的商品 -->
          <td width="30%" bgcolor="#EEE" align="center">
             <br>
             <b><font color="#FF7F00">您浏览过的商品</font></b><br>
             <!-- 循环开始 -->
             <% 
             ArrayList<Goods> itemsList =goodDao.getViewList(list);
				if(itemsList != null && itemsList.size() > 0) {
					for(Goods i : itemsList) {

             %>
             <div>
             <dl>
               <dt>
                 <a href="details.jsp?id=<%=i.getId()%>"><img src="images/<%=i.getPicture() %>" width="120" height="90" border="1"/></a>
               </dt>
               <dd class="dd_name"><%=i.getName() %></dd> 
               <dd class="dd_city">产地:<%=i.getCity() %>&nbsp;&nbsp;价格:￥<%=i.getPrice() %> </dd> 
             </dl>
             </div>
             <% 
                   }
                }
             %>
             <!-- 循环结束 -->
          </td>
        </tr>
    </table>
    </center>
</body>
</html>